package com.example.guetdemo.controller;

import com.alibaba.fastjson.JSON;
import com.example.guetdemo.annotations.PhoneValidation;
import com.example.guetdemo.annotations.VerificationCodeValidation;
import com.example.guetdemo.domain.dto.CodeDto;
import com.example.guetdemo.domain.dto.LoginDto;
import com.example.guetdemo.domain.dto.RegisterDto;
import com.example.guetdemo.result.ResponseResult;
import com.example.guetdemo.service.UserService;
import com.example.guetdemo.utils.WebUtils;
import com.google.zxing.WriterException;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.validation.Valid;
import java.io.IOException;


@RestController
@RequestMapping("/users")
public class UserController {

    @Resource
    private UserService userService;


    @RequestMapping("/hello")
    public String hello() {
        return "hello";
    }

    /**
     * 注册
     * @param registerDto
     * @return
     */
    @PostMapping
    public ResponseResult register(@Valid @RequestBody RegisterDto registerDto , HttpServletResponse httpServletResponse) {
        ResponseResult result = userService.register(registerDto);
        WebUtils.setStatus(httpServletResponse,result.getCode());
        return result;
    }

    /**
     * 登录
     * @param loginDto
     * @return
     */
    @PostMapping("/login")
    public ResponseResult userLogin(@Valid @RequestBody LoginDto loginDto, HttpSession httpSession, HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse){
        return userService.userLogin(loginDto,httpSession,httpServletRequest,httpServletResponse);
    }

    /**
     * 认证测试
     * @return
     */
    @GetMapping
    public ResponseResult getUserList(HttpServletResponse httpServletResponse){
        ResponseResult result = userService.getUserList();
        WebUtils.setStatus(httpServletResponse,result.getCode());
        return result;
    }

    /**
     * 模拟发送验证码，并存储验证码
     * @param codeDto
     * @return
     */
    @PostMapping("/code")
    public ResponseResult sendCode(@Validated(PhoneValidation.class) @RequestBody CodeDto codeDto, HttpServletResponse httpServletResponse) {
        ResponseResult result = userService.sendCode(codeDto);
        WebUtils.setStatus(httpServletResponse, result.getCode());
        return result;
    }

    /**
     * 校验验证码
     * @param codeDto
     * @param httpServletResponse
     * @return
     */
    @PostMapping("/phone-code")
    public ResponseResult verifyCode(@Validated(VerificationCodeValidation.class) @RequestBody CodeDto codeDto, HttpServletResponse httpServletResponse) {
        ResponseResult result = userService.verifyCode(codeDto);
        WebUtils.setStatus(httpServletResponse,result.getCode());
        return result;
    }


    @PostMapping("/test2")
    public String get(@RequestBody LoginDto loginDto, HttpServletRequest httpServletRequest , HttpServletResponse httpServletResponse) {
        String string = JSON.toJSONString(loginDto);
        httpServletResponse.setHeader("Blocker-Type","success");
        return httpServletResponse.getHeader("Blocker-Type");
    }

    /**
     * 根据 uuid生成二维码
     * @return
     */
    @GetMapping("/qrcode-uuid")
    public ResponseEntity<byte[]> generateQRUUID(HttpServletResponse httpServletResponse, @RequestParam(value = "logoUrl", required = false) String logoUrl) throws WriterException, IOException {
        return userService.generateQRUUID(httpServletResponse);
    }

    /**
     * 扫描二维码二维码状态改变
     * @param httpServletResponse
     * @return
     */
    @PatchMapping("/qrcode-scan")
    public ResponseResult scanQRCode(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws IOException {
        return userService.scanQRCode(httpServletRequest,httpServletResponse);
    }

    /**
     * 轮询方式确认二维码登录
     * @param httpServletRequest
     * @param httpServletResponse
     * @return
     */
    @PatchMapping("/qrcode-login")
    public ResponseResult confirmQRLogin(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) {
        return userService.confrimQRLogin(httpServletRequest,httpServletResponse);
    }

    /**
     * websocket方式确认二维码登录
     * @param httpServletRequest
     * @param httpServletResponse
     * @return
     */
    @PatchMapping("/qrcode-login-websocket")
    public ResponseResult confirmQRLoginWebsocket(HttpSession httpSession , HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws IOException {
        return userService.confirmQRLoginWebsocket(httpSession , httpServletRequest,httpServletResponse);
    }


    /**
     * 客户端轮询二维码状态
     * @param httpServletRequest
     * @return
     */
    @GetMapping("/qrcode-status")
    public ResponseResult checkQRCode(HttpSession httpSession, HttpServletRequest httpServletRequest,HttpServletResponse httpServletResponse) {
        return userService.checkQRCode(httpSession,httpServletRequest,httpServletResponse);
    }

}
